#!/usr/bin/env python3
"""
非异步版本的测试脚本
"""

import sys
import os

# 确保项目根目录在 Python 路径中
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
    sys.path.insert(0, project_root)

print(f"项目根目录: {project_root}")

try:
    from app.config import settings
    from pydiscourse import DiscourseClient
    
    print(f"\n配置加载成功:")
    print(f"  Base URL: {settings.DISCOURSE_BASE_URL}")
    print(f"  Username: {settings.DISCOURSE_USERNAME}")
    print(f"  API Key 前10位: {settings.DISCOURSE_API_KEY[:10]}...")
    
except ImportError as e:
    print(f"导入配置失败: {e}")
    sys.exit(1)

def sync_test():
    """同步测试"""
    print("\n=== 同步标签搜索测试 ===")
    
    try:
        # 直接使用 pydiscourse 客户端
        client = DiscourseClient(
            host=settings.DISCOURSE_BASE_URL,
            api_key=settings.DISCOURSE_API_KEY,
            api_username=settings.DISCOURSE_USERNAME
        )
        
        print("DiscourseClient 创建成功")
        
        # 测试搜索
        tag_query = "#spring"
        print(f"搜索查询: {tag_query}")
        
        results = client.search(tag_query, limit=5)
        posts = results.get('posts', [])
        
        print(f"搜索结果: {len(posts)} 个帖子")
        
        if posts:
            for i, post in enumerate(posts[:3], 1):
                print(f"  帖子 {i}:")
                print(f"    ID: {post.get('id')}")
                print(f"    用户: {post.get('username')}")
                print(f"    Topic ID: {post.get('topic_id')}")
                print(f"    创建时间: {post.get('created_at')}")
        else:
            print("  没有找到任何帖子")
            print(f"  完整结果结构: {list(results.keys())}")
        
    except Exception as e:
        print(f"测试失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    sync_test()
